package com.yuncheng.spcyApi.service.impl;

import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcyRsJcyqjgl;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SpcyRsJcyqjglMapper;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.ISpcyRsJcyqjglService;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.vo.CurrentUserVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @Description: 审评查验-检查员请假管理
 * @Author: yuncheng
 * @Date: 2023-10-13
 * @Version: V1.0
 */
@Service
public class SpcyRsJcyqjglServiceImpl extends BaseServiceImpl<SpcyRsJcyqjglMapper, SpcyRsJcyqjgl> implements ISpcyRsJcyqjglService {

    @Resource
    private ISpcyLogService spcyLogService;

    private final static String YXZT_YX = "有效";
    private final static String YXZT_WX = "无效";

    @Override
    public SpcyRsJcyqjgl addQj(SpcyRsJcyqjgl qjjl) {
        qjjl.setId(GetUuIdUtils.ReplaceUuId());
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            throw new BusinessException("登录已失效！请重新登录！");
        }
        qjjl.setFuserid(currentUser.getId());
        qjjl.setFxm(currentUser.getName());
        qjjl.setFstate(YXZT_YX);
        boolean save = save(qjjl);
        if (!save){
            throw new BusinessException("保存失败！请刷新重试！");
        }

        String log = "新增检查员请假：";
        log += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(qjjl));
        spcyLogService.addLogs(LogConstant.LOGS_JCYGL,log,qjjl.getFuserid(), "新增检查员请假",SpcyConstant.LOGS_SYS_TYPE_XZ);

        return qjjl;
    }

    @Override
    public void updateQj(SpcyRsJcyqjgl qjjl) {
        if (StringUtils.isBlank(qjjl.getId())){
            throw new BusinessException("保存参数丢失！请联系管理员！");
        }
        SpcyRsJcyqjgl oldQj = getById(qjjl.getId());
        if (oldQj == null){
            throw new BusinessException("保存失败！当前数据不存在！请刷新重试！");
        }
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(oldQj);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        try {
            BeanUtils.populate(oldQj, BeanUtils.describe(qjjl));
            if (qjjl.getFqjkssj() != null){
                oldQj.setFqjkssj(qjjl.getFqjkssj());
            }
            if (qjjl.getFqjjssj() != null){
                oldQj.setFqjjssj(qjjl.getFqjjssj());
            }
        } catch (IllegalAccessException e) {
            throw new BusinessException("保存失败！[IllegalAccessException]: "+e.getMessage());
        } catch (InvocationTargetException e) {
            throw new BusinessException("保存失败！[InvocationTargetException]: "+e.getMessage());
        } catch (NoSuchMethodException e) {
            throw new BusinessException("保存失败！[NoSuchMethodException]: "+e.getMessage());
        }

        updateById(oldQj);

        String operContent = "检查员请假信息修改：";
        operContent += LogUtils.BeanModifyMsg(oldQj,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_JCYGL,operContent,oldQj.getFuserid(), "检查员请假信息修改",SpcyConstant.LOGS_SYS_TYPE_XG);

    }

    @Override
    public void deleteQj(String ids) {
        if (StringUtils.isBlank(ids)){
            throw new BusinessException("删除失败！请选择！");
        }
        List<String> listByIds = StringPatternUtils.getListByIds(ids);

        List<SpcyRsJcyqjgl> list = listByIds(listByIds);
        for (int i=0; i<list.size(); i++){
            removeById(list.get(i).getId());

            String operContent = "删除检查员请假信息：";
            operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(list.get(i)));
            spcyLogService.addLogs(LogConstant.LOGS_JCYGL,operContent,list.get(i).getFuserid(),"删除检查员请假信息",SpcyConstant.LOGS_SYS_TYPE_SC);
        }

    }

    @Override
    public void cancelQj(String ids) {
        if (StringUtils.isBlank(ids)){
            throw new BusinessException("取消失败！请选择！");
        }
        List<String> listByIds = StringPatternUtils.getListByIds(ids);

        List<SpcyRsJcyqjgl> list = listByIds(listByIds);
        for (int i=0; i<list.size(); i++){
            SpcyRsJcyqjgl updateQj = list.get(i);
            updateQj.setFstate(YXZT_WX);
            updateById(updateQj);

            String operContent = "取消检查员请假：";
            operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(list.get(i)));
            spcyLogService.addLogs(LogConstant.LOGS_JCYGL,operContent,list.get(i).getFuserid(),"取消检查员请假",SpcyConstant.LOGS_SYS_TYPE_SC);
        }
    }
}
